const app = getApp();
const util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "记事本",
    isTransform: false,
    isBack: true,
    CustomBar: app.globalData.CustomBar,
    notelist: [],
    loadModal: true,
    logining:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(util.isLoged());
    if(util.isLoged()){
      this.getNodeList();
    } else {
      this.setData({
        logining:'show',
        loadModal:false
      })
    }
  },

  onShow: function (options) {
    if(util.isLoged()){
      this.getNodeList();
    } else {
      this.setData({
        logining:'show',
        
      })
    }
  },


  getNodeList(thing) {
    let that = this
    that.setData({
      loadModal: true
    })
    wx.cloud.callFunction({
      name: "getOpenid",
      success(res) {
        wx.cloud.callFunction({
          // 需调用的云函数名
          name: 'getNoteList',
          // 传给云函数的参数
          data: {
            openid: res.result.openid,
            thing: thing
          },
          success: function (res) {
            console.log(res.result.data);
            that.setData({
              notelist: res.result.data,
              loadModal: false
            })
          },
          fail: function (err) {
            console.log(err);
          }
        })
      }
    })
  },

  search(e) {
    this.getNodeList(e.detail.value);
  },

  toDetail(e) {
    let id = e.currentTarget.id;
    let that = this;
    let notelist = that.data.notelist[id];
    let openid = notelist._openid;
    let ids = notelist._id;
    let thing = notelist.thing;
    let context = notelist.context;
    let date = notelist.date;
    let time = notelist.time;
    wx.navigateTo({
      url: '../notepadetail/notepadetail?id=' + ids + '&openid=' + openid + '&thing=' + thing + '&context=' + context + '&date=' + date + '&time=' + time,
    })
  },
  toNote() {
    if(util.isLoged()){
      wx.navigateTo({
        url: '../notepadlist/notepadlist',
      })
    } else{
      this.setData({
        logining:'show'
      })
    }
  }

})